package com.designoutput.ui
{
	import com.designoutput.design.LongBackSide;
	import com.designoutput.design.LongFrontSide;
	import com.designoutput.design.LongLeftSide;
	import com.designoutput.design.LongRightSide;
	import com.designoutput.design.ShortBackSide;
	import com.designoutput.design.ShortFrontSide;
	import com.designoutput.design.ShortLeftSide;
	import com.designoutput.design.ShortRightSide;
	import com.designoutput.elements.Base;
	import com.designoutput.elements.BaseElement;
	import com.designoutput.elements.EllipseElement;
	import com.designoutput.elements.ImageElement;
	import com.designoutput.elements.LineElement;
	import com.designoutput.elements.PolyLineElement;
	import com.designoutput.elements.RectElement;
	import com.designoutput.elements.RoundRectElement;
	import com.designoutput.elements.StarElement;
	import com.designoutput.elements.TextElement;
	import com.designoutput.enum.CardType;
	import com.designoutput.enum.ElementType;
	import com.designoutput.enum.Orientation;
	import com.designoutput.enum.ToolType;
	import com.designoutput.event.CardEvent;
	import com.designoutput.event.CardLayerEvent;
	import com.designoutput.utils.Globals;
	import com.designoutput.utils.PDFUtils;
	import com.designoutput.utils.Unit;
	import com.designoutput.utils.UnitUtils;
	import com.designoutput.vo.LayerTypeVo;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Matrix;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	public class CardFace extends Base
	{
		private var _card:Card;
		
		public var overlay:Sprite;
				
		private var _layerType:LayerTypeVo;
		
		private var _unit:Unit = Unit.INCHES;
		private var _widthInUnit:Number;
		private var _heightInUnit:Number;
		private var _orientation:String;
		
		private var _shape:DisplayObject;
		private var _shapeBytes:ByteArray;
		private var _currentShape:String = "";
		
		private var _backgroundColor:uint = 0xFFFFFF;
		private var _backgroundImageURL:String;
		private var _backgroundImageBytes:ByteArray;
		private var _backgroundImage:DisplayObject;
		private var _backgroundImageURLLoader:URLLoader;
		private var _backgroundImageLoader:Loader;
		
		private var _backgroundHolder:Sprite;
		private var _content:Sprite;
		private var _margins:Sprite;
		
		public var print:Boolean = true;		
		private var _viewing:Boolean = false;
		
		public function CardFace(parent:Card, layerVo:LayerTypeVo)
		{
			super();
			
			_card = parent;
			
			name = layerVo.name;
			setLayerTypeVo(layerVo);
			
			_height = 30;
			_backgroundImageURL = "";
			_backgroundColor = 0xffffff;
			
			_backgroundHolder = new Sprite();
			addChild(_backgroundHolder);
			
			_content = new Sprite();
			addChild(_content);
			
			overlay = new Sprite();
			addChild(overlay);
			
			_margins = new Sprite();
			_margins.mouseEnabled = false;
			addChild(_margins);
		}
		
		public function get viewing():Boolean {
			return _viewing;
		}
		public function set viewing(value:Boolean):void {
			_viewing = value;
			dispatchEvent(new CardLayerEvent(CardLayerEvent.VIEWING_CHANGED));
		}
		
		
		public function get unit():Unit {
			return _unit;
		}
		
		public function get orientation():String {
			return _orientation;
		}
		
		public function get widthInUnit():Number {
			return _widthInUnit;
		}
		
		public function get heightInUnit():Number {
			return _heightInUnit;
		}
		
		public function get sizeName():String {
			return _layerType.sizeName;
		}
		
		public function get allowCustomSize():Boolean {
			return _layerType && _layerType.allowCustomSize;
		}
		
		public function get allowBackgroundColor():Boolean {
			return _layerType && _layerType.allowBackgroundColor;
		}
		
		public function get allowBackgroundImage():Boolean {
			return _layerType && _layerType.allowBackgroundImage;
		}
		
		public function getClassName():String
		{
			return "CardLayer";
		}
		
		public function getBackgroundColor():uint
		{
			return _backgroundColor;
		}
		
		public function setBackgroundColor(value:uint):void
		{
			_backgroundColor = value;
			invalidateDisplay();
			
			dispatchEvent( new CardLayerEvent(CardLayerEvent.BACKGROUND_COLOR_CHANGED) );
		}
		
		public function setLayerTypeVo(layerVo:LayerTypeVo):void {
			_layerType = layerVo;
			changeUnit(Unit.INCHES);
			changeSize(_layerType.width, _layerType.height);
			
			if(!_layerType.allowBackgroundImage)
				removeBgImage();
			
			if(!_layerType.allowBackgroundColor)
				setBackgroundColor(0xFFFFFF);
		}
		
		public function changeSize(w:Number, h:Number):void {
			w = Math.max(UnitUtils.convert(0.5, Unit.INCHES, unit), w);
			w = Math.min(UnitUtils.convert(83.33, Unit.INCHES, unit), w);
			
			h = Math.max(UnitUtils.convert(0.5, Unit.INCHES, unit), h);
			h = Math.min(UnitUtils.convert(83.33, Unit.INCHES, unit), h);
			
			_widthInUnit = w;
			_heightInUnit = h;
			
			_orientation = w > h ? Orientation.LANDSCAPE : Orientation.PORTRAIT;
			
			width = UnitUtils.convert(_widthInUnit, unit, Unit.PIXELS);
			height = UnitUtils.convert(_heightInUnit, unit, Unit.PIXELS);
			
			dispatchEvent( new CardLayerEvent( CardLayerEvent.SIZE_CHANGED ) );
			
			invalidateDisplay();
		}
		
		public function changeUnit(newUnit:Unit):void {
			if(_unit != newUnit){
				_widthInUnit =  UnitUtils.convert(_widthInUnit, _unit, newUnit, 2);
				_heightInUnit = UnitUtils.convert(_heightInUnit, _unit, newUnit, 2);
				
				_unit = newUnit;
				
				dispatchEvent( new CardEvent( CardEvent.UNIT_CHANGED ) );
			}
		}
		
		/**
		 * To load background image for the card layer
		 * @param url
		 */
		public function setBackgroundImage(url:String):void
		{
			if(!allowBackgroundImage)
				return;
			
			_card.setPendingChanges();
			
			removeBgImage();
			
			_backgroundImageURL = url;
			
			if(url != null && url != ""){
				var request:URLRequest = new URLRequest(Globals.applicationUrl + url);
				_backgroundImageURLLoader = new URLLoader();
				_backgroundImageURLLoader.addEventListener(Event.COMPLETE, backgroundImageURLLoader_completeHandler, false, 0, true);
				_backgroundImageURLLoader.dataFormat = URLLoaderDataFormat.BINARY;
				_backgroundImageURLLoader.load(request);
			}
		}
		
		private function backgroundImageURLLoader_completeHandler(e:Event):void {
			_backgroundImageBytes = _backgroundImageURLLoader.data;
			_backgroundImageURLLoader = null;
			
			_backgroundImageLoader = new Loader();
			_backgroundImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, backgroundImageLoader_completeHandler);
			_backgroundImageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, backgroundImage_loadError);
			_backgroundImageLoader.loadBytes(_backgroundImageBytes);
		}
		
		private function backgroundImageLoader_completeHandler(e:Event):void
		{
			_backgroundImage = _backgroundImageLoader.content;
			_backgroundHolder.addChild(_backgroundImage);
			_backgroundImageLoader = null;
			invalidateDisplay();
		}
		
		private function backgroundImage_loadError(evt:Event):void
		{
			_backgroundImageBytes = null;
			_backgroundImageURLLoader = null;
			_backgroundImageLoader = null;
			this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, ErrorEvent(evt).text));
		}
		
		public function removeBgImage():void
		{
			if (_backgroundImage != null && _backgroundHolder.contains(_backgroundImage))
				_backgroundHolder.removeChild(_backgroundImage);
			
			_backgroundImageURL = "";
		}
		
		public function getBgImageURL():String
		{
			return _backgroundImageURL;
		}
		
		/**
		 * Remove all children
		 * <BR><B>NOTE</B>: This should be called for Sleeve Layers, since when switch views for Shirt, the coordinate yet be changed.
		 * So, just remove them.
		 */
		public function removeAllElements():void
		{
			while(_content.numChildren > 0)
			{
				var baseElement:BaseElement = BaseElement(_content.getChildAt(0));
				if(baseElement)
					removeElement(baseElement);
			}
		}
		
		public function wrapX(nVal:Number):Number
		{
			return Math.max(-_width, Math.min(_width * 2, nVal));
		}
		
		public function wrapY(nVal:Number):Number
		{
			return Math.max(-_height, Math.min(_height * 2, nVal));
		}
		
		/**
		 * Update the back image position and size. 
		 * @param isTShirt
		 * @param bothMode
		 * 
		 */		
		public function updateBackgroundImageSize(isTShirt:Boolean = false, bothMode:Boolean = false):void
		{
			if (this._backgroundImage)
			{
				if (!isTShirt)
				{
					_backgroundImage.width = _width + 1;
					_backgroundImage.height = _height + 1;
					_backgroundImage.x = 0;
					_backgroundImage.y = 0;
				}
				else
				{
					if (!bothMode)
					{
						_backgroundImage.width = 292;
						_backgroundImage.height = 468;
						_backgroundImage.x = 127;
						_backgroundImage.y = 51;
					}else{
						_backgroundImage.width = 292;
						_backgroundImage.height = 468;
						_backgroundImage.x= 127;
						_backgroundImage.y= 51;
					}
				}
			}
		}
		
		public function changeElementDepth(layoutType:String, selItems:Array):void
		{
			var currentIndex:int = _content.getChildIndex(selItems[0]);
			var targetIndex:int = currentIndex;
			
			switch (layoutType)
			{
				case ToolType.BringToFront:
					targetIndex = _content.numChildren - 1;
					break;
				case ToolType.BringForward:
					if (currentIndex < _content.numChildren - 1)
						targetIndex = currentIndex + 1;
					break;
				case ToolType.SendBackward:
					if (currentIndex > 0)
						targetIndex = currentIndex - 1;
					break;
				case ToolType.SendToBack:
					targetIndex = 0;
					break;
			}
			
			if(targetIndex != currentIndex){
				var element:DisplayObject = _content.getChildAt(currentIndex);
				_content.setChildIndex(element, targetIndex);
			}
		}
		
		public function onRestoreProportion():void
		{
			var elem:com.designoutput.elements.ImageElement = _card.getSelectItems()[0];
			if (elem is com.designoutput.elements.ImageElement)
			{
				elem = elem as com.designoutput.elements.ImageElement;
				if (elem.getOriginalProportion() > 0)
				{
					if (elem.height / elem.width < elem.getOriginalProportion())
					{
						elem.width = elem.height / elem.getOriginalProportion();
					} else {
						elem.height = elem.width * elem.getOriginalProportion();
					}
					
					elem.validate();
				}
			}
		}
		
		public function createElement(elementType:String):BaseElement
		{
			var element:BaseElement;
			switch (elementType)
			{
				case ElementType.RectangleElement:
					element = new RectElement();
					break;
				case ElementType.RoundRectangleElement:
					element = new RoundRectElement();
					break;
				case ElementType.EllipseElement:
					element = new EllipseElement();
					break;
				case ElementType.StarElement:
					element = new StarElement();
					break;
				case ElementType.PolyLineElement:
					element = new PolyLineElement();
					break;
				case ElementType.LineElement:
					element = new LineElement();
					break;
				case ElementType.TextElement:
					element = new TextElement();
					break;
				case ElementType.ImageElement:
					element = new com.designoutput.elements.ImageElement();
					break;
				default:
					break;
			}
			// push new element once new one created
			addElement(element);
			return element;
		}
		
		public function get numElements():int {
			return _content.numChildren;
		}
		
		public function getElementAt(index:int):BaseElement {
			return _content.getChildAt(index) as BaseElement;
		}
		
		public function addElement(element:BaseElement):void
		{
			_content.addChild(element);
		}
		
		public function removeElement(element:BaseElement):void
		{
			_content.removeChild(element);
		}
		
		public function pasteElementsXML(elements:XMLList):Array
		{
			var arrItems:Array = addElementsXML(elements);
			for each(var element:BaseElement in arrItems){
				element.x += 10;
				element.y += 10;
			}
			return arrItems;
		}
		
		private function addElementsXML(elements:XMLList):Array {
			var arrItems:Array = new Array();
			for (var j:int = 0; j < elements.length(); j++)
			{
				var element:BaseElement = createElement(elements[j].localName());
				element.fromXML(elements[j]);
				arrItems.push(element);
			}
			
			return arrItems;
		}
		
		public function toXML():XML
		{
			var layerXML:XML = <{getClassName()} />;
			
			layerXML.@name = name;
			
			layerXML.@bgImageURL = _backgroundImageURL;
			layerXML.@bg = _backgroundColor;
			
			if(_layerType.shape)
				layerXML.@shape = _layerType.shape;
			
			layerXML.@w = UnitUtils.convert(_widthInUnit, _unit, Unit.INCHES);
			layerXML.@h = UnitUtils.convert(_heightInUnit, _unit,Unit.INCHES);
			
			layerXML.@print = print;
			
			var elementsNode:XML = <elements />;
			for(var i:int = 0; i < _content.numChildren; i++){
				var element:BaseElement = _content.getChildAt(i) as BaseElement;
				if(element)
					elementsNode.appendChild(element.toXML());
			}
			layerXML.appendChild(elementsNode);
			
			return layerXML;
		}
		
		public function fromXML(xmlNode:XML):void
		{
			removeAllElements();
			
			if("@w" in xmlNode && "@h" in xmlNode)
			{
				var cardWidth:Number = UnitUtils.convert(Number(xmlNode.@w), Unit.INCHES, unit);
				var cardHeight:Number = UnitUtils.convert(Number(xmlNode.@h), Unit.INCHES, unit);
				changeSize(cardWidth, cardHeight);;
			}
			
			print = xmlNode.@print != "false";
			
			setBackgroundImage(xmlNode.@bgImageURL);
			setBackgroundColor(uint(xmlNode.@bg));
			
			addElementsXML(xmlNode.elements.elements());
		}
		
		public function get showMargins():Boolean
		{
			return _margins.visible;
		}
		
		public function set showMargins(value:Boolean):void {
			_margins.visible = value;
		}
		
				override protected function updateDisplay():void {
			super.updateDisplay();
			
			drawCard();
			drawMargins();
			updateShape();
			
			if (_backgroundImage)
			{
				_backgroundImage.width = _width;
				_backgroundImage.height = _height;
				_backgroundImage.x = 0;
				_backgroundImage.y = 0;
			}
		}
				
		
		private function updateShape():void {
			if(_layerType.shape != _currentShape){				
				if(_shape != null){
					_backgroundHolder.removeChild(_shape);
					_shape = null;
				}
				
				_currentShape = _layerType.shape;
				
				switch(_layerType.shape){
					case "ShortFrontSide" :
						_shape = new ShortFrontSide();
						break;
					case "ShortBackSide" :
						_shape = new ShortBackSide();
						break;
					case "ShortLeftSide" :
						_shape = new ShortLeftSide();
						break;
					case "ShortRightSide" :
						_shape = new ShortRightSide();
						break;
					
					case "LongFrontSide" :
						_shape = new LongFrontSide();
						break;
					case "LongBackSide" :
						_shape = new LongBackSide();
						break;
					case "LongLeftSide" :
						_shape = new LongLeftSide();
						break;
					case "LongRightSide" :
						_shape = new LongRightSide();
						break;
				}
				
				if(_shape)
					_backgroundHolder.addChildAt(_shape, 0);	
			}
		}
		
		private function drawMargins():void
		{
			while (_margins.numChildren)
			{
				/**Margins will add child when draw for Triangle*/
				_margins.removeChildAt(0);
			}
			var layerType:String = _layerType.type.toUpperCase();
			_margins.graphics.clear();
			switch (layerType)
			{
				case CardType.CARD_TYPE_BUTTON:
					drawOvalMargins();
					break;
				case CardType.CARD_TYPE_TRI_FOLDER:
					drawTriFolderMargins();
					break;
				case CardType.CARD_TYPE_TRIANGLE:
					drawTriangleMargins();
					break;
				case CardType.CARD_TYPE_T_SHIRTS:
					drawTShirtMargins();
					break;
				case CardType.CARD_TYPE_SIMPLE:
				default:
					drawCommonMargins();
					break;
			}
		}
		
		
		private function drawCard():void
		{
			var layerType:String = _layerType.type.toUpperCase();
			
			this.graphics.clear();
			switch (layerType)
			{
				case CardType.CARD_TYPE_BUTTON:
					drawOvalCard();
					break;
				case CardType.CARD_TYPE_TRIANGLE:
					drawTriangleCard();
					break;
				case CardType.CARD_TYPE_T_SHIRTS :
					break;
				case CardType.CARD_TYPE_SIMPLE :
				default:
					drawCommonCard();
					break;
			}
		}
		
		
		
		/***************************************************************************
		 * ***************************************************************************
		 * ***************************************************************************
		 * ************************** DRAWING CARD AND MARGINS *********************
		 ***************************************************************************/
		
		/*************************** draw Card body **********************************/
		private function drawOvalCard():void
		{
			fillOval(this, _width / 2, _height / 2, _width / 2, _height / 2, _backgroundColor);
		}
		
		private function drawTriangleCard():void
		{
			drawTriangle(this, 1, 1, _width + 1, _height + 1, 0x000000);
			drawTriangle(this, 0, 0, _width, _height, _backgroundColor);
		}
		
		private function drawCommonCard():void
		{
			drawRect(this, 1, 1, _width + 1, _height + 1, 0x000000);
			drawRect(this, 0, 0, _width, _height, _backgroundColor);
		}
		
		/*************************** draw Margins **********************************/
		private function drawTShirtMargins():void
		{
			_margins.graphics.clear();
			
			switch(_layerType.shape){
				case "LongFrontSide" :
				case "LongBackSide" :
					drawFrameRect(_margins, 144, 66, 402, 498, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
					drawFrameRect(_margins, 136, 58, 410, 506, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
					break;
				case "ShortFrontSide" :
				case "ShortBackSide" :
					drawFrameRect(_margins, 144, 66, 402, 502, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
					drawFrameRect(_margins, 136, 58, 410, 510, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
					break;
				case "LongLeftSide" :
					drawFrameRect(_margins, 122, 98, 188, 518, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
					drawFrameRect(_margins, 114, 90, 196, 526, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
					break;
				case "ShortLeftSide" :
					drawFrameRect(_margins, 122, 98, 188, 186, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
					drawFrameRect(_margins, 114, 90, 196, 192, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
					break;
				case "LongRightSide" :
					drawFrameRect(_margins, 28, 98, 106, 518, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
					drawFrameRect(_margins, 20, 90, 114, 526, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
					break;
				case "ShortRightSide" :
					drawFrameRect(_margins, 28, 98, 106, 184, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
					drawFrameRect(_margins, 20, 90, 114, 192, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
					break;
			}
		}
		
		private function drawOvalMargins():void
		{
			_margins.graphics.clear();
			var outerMargin:Number = UnitUtils.convert(Globals.designoutputCard.cardTypeCollection.outerMargin, Unit.INCHES,  Unit.PIXELS);
			var innerMargin:Number = UnitUtils.convert(_layerType.marginTop, Unit.INCHES,  Unit.PIXELS);
			drawEllipse(_margins, _width / 2, _height / 2, _width / 2 - innerMargin, _height / 2 - innerMargin, Globals.designoutputCard.cardTypeCollection.innerMarginColor, 5);
			drawEllipse(_margins, _width / 2, _height / 2, _width / 2 - outerMargin, _height / 2 - outerMargin, Globals.designoutputCard.cardTypeCollection.outerMarginColor, 5);
			
		}
		
		private function getTriangleLeftBottomMargin(_margins:Number):Number
		{
			var a:Number = (Math.atan(Math.round(2 * _height / _width)));
			var b:Number = a / 2;
			var p:Number = Math.round(_margins / Math.tan(b));
			return p;
		}
		
		private function drawTriangleMargins():void
		{
			var outerMargin:Number = UnitUtils.convert(Globals.designoutputCard.cardTypeCollection.outerMargin, Unit.INCHES,  Unit.PIXELS);
			var innerMargin:Number = UnitUtils.convert(_layerType.marginTop, Unit.INCHES,  Unit.PIXELS);
			
			var p:* = this.getTriangleLeftBottomMargin(outerMargin);
			var scale:Number = (_width - 2 * p) / _width;
			var marginsWidth1:Number = scale * _width;
			var marginsHeight1:Number = scale * _height;
			var nX1:* = 0 + (_width - marginsWidth1) / 2;
			var nY1:* = _height - marginsHeight1 - outerMargin;
			var nX2:* = _width - p;
			var nY2:* = _height - outerMargin;
			drawFrameTriangleDotted(_margins, nX1, nY1, nX2, nY2, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
			
			p = this.getTriangleLeftBottomMargin(innerMargin);
			scale = (_width - 2 * p) / _width;
			marginsWidth1 = scale * _width;
			marginsHeight1 = scale * _height;
			nX1 = 0 + (_width - marginsWidth1) / 2;
			nY1 = _height - marginsHeight1 - innerMargin;
			nX2 = _width - p;
			nY2 = _height - innerMargin;
			drawFrameTriangleDotted(_margins, nX1, nY1, nX2, nY2, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
		}
		
		private function drawTriFolderMargins():void
		{
			var outerMargin:Number = UnitUtils.convert(Globals.designoutputCard.cardTypeCollection.outerMargin, Unit.INCHES,  Unit.PIXELS);
			var marginTop:Number = UnitUtils.convert(_layerType.marginTop, Unit.INCHES,  Unit.PIXELS);
			var marginRight:Number = UnitUtils.convert(_layerType.marginRight, Unit.INCHES,  Unit.PIXELS);
			var marginBottom:Number = UnitUtils.convert(_layerType.marginBottom, Unit.INCHES,  Unit.PIXELS);
			var marginLeft:Number = UnitUtils.convert(_layerType.marginLeft, Unit.INCHES,  Unit.PIXELS);
			
			var iM:Number = (marginLeft + marginRight) / 2;
			var outerLeft:Number = 0;
			var outerTop:Number = 0;
			var outerRight:Number = _width - 2 + outerLeft;
			var outerBottom:Number = _height - 2 + outerTop;
			var w:Number = outerRight - outerLeft;
			var panelW:Number = (w - 4 * iM) / 3;
			drawFrameRect(_margins, outerLeft + outerMargin, outerTop + outerMargin, outerRight - outerMargin, outerBottom - outerMargin, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
			
			var panelLeft:Number = outerLeft + iM;
			var panelTop:Number = outerTop + marginTop;
			var panelRight:Number = outerLeft + iM + panelW;
			var panelBottom:Number = outerTop + outerBottom - marginBottom;
			drawFrameRect(_margins, panelLeft, panelTop, panelRight, panelBottom, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
			
			panelLeft = panelRight + iM;
			panelTop = outerTop + marginTop;
			panelRight = panelLeft + panelW;
			panelBottom = outerTop + outerBottom - marginBottom;
			drawFrameRect(_margins, panelLeft, panelTop, panelRight, panelBottom, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
			
			panelLeft = panelRight + iM;
			panelTop = outerTop + marginTop;
			panelRight = panelLeft + panelW;
			panelBottom = outerTop + outerBottom - marginBottom;
			drawFrameRect(_margins, panelLeft, panelTop, panelRight, panelBottom, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
		}
		
		private function drawCommonMargins():void
		{
			var outerMargin:Number = UnitUtils.convert(Globals.designoutputCard.cardTypeCollection.outerMargin, Unit.INCHES,  Unit.PIXELS);
			var marginTop:Number = UnitUtils.convert(_layerType.marginTop, Unit.INCHES,  Unit.PIXELS);
			var marginRight:Number = UnitUtils.convert(_layerType.marginRight, Unit.INCHES,  Unit.PIXELS);
			var marginBottom:Number = UnitUtils.convert(_layerType.marginBottom, Unit.INCHES,  Unit.PIXELS);
			var marginLeft:Number = UnitUtils.convert(_layerType.marginLeft, Unit.INCHES,  Unit.PIXELS);
			drawFrameRect(_margins, marginLeft, marginTop, _width - marginRight, _height - marginBottom, Globals.designoutputCard.cardTypeCollection.innerMarginColor);
			drawFrameRect(_margins, outerMargin, outerMargin, _width - outerMargin, _height - outerMargin, Globals.designoutputCard.cardTypeCollection.outerMarginColor);
		}
	}
}